//
//  hotDetailController.m
//  YouGoWeGo
//
//  Created by 范业君 on 14-11-22.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "hotDetailController.h"
#import "detailTableViewCell.h"
#import "detaiTriplModel.h"

@interface hotDetailController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation hotDetailController


- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self createTableView];
    
    [self startAdyn];
  
}


#pragma mark -
#pragma mark 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [_tableView registerClass:[detailTableViewCell class] forCellReuseIdentifier:@"ok"];
    //[_tableView setContentSize:CGSizeMake(1000, 0)];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setSeparatorColor:[UIColor redColor]];
    [self.view addSubview:_tableView];
    [_tableView release];
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok"];
    detaiTriplModel *model = [_tableArray objectAtIndex:indexPath.row];
    cell.model = model;
    
    
    return cell;
}

- (void)startAdyn
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%ld/waypoints/",self.numid]];
    
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *dayArray = [dic objectForKey:@"days"];
        
        for (NSMutableDictionary *dic1 in dayArray) {
            NSMutableArray *arr = [dic1 objectForKey:@"waypoints"];
            
            for (NSMutableDictionary *dic2 in arr) {
                detaiTriplModel *model = [[detaiTriplModel alloc] init];
                [model setValuesForKeysWithDictionary:dic2];
                
                [_tableArray addObject:model];
                [_tableView reloadData];
                [model release];
            }
            
        }
    }];
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    detaiTriplModel *model = [_tableArray objectAtIndex:indexPath.row ];
    NSMutableDictionary *dic = model.photo_info;
    
    if (dic == nil) {
        return 0;
    }
    UILabel *label = [[UILabel alloc] init];
    CGRect rect = [model.text boundingRectWithSize:CGSizeMake(375, 300) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    [label setFrame:CGRectMake(0, 0, self.view.frame.size.width, rect.size.height)];
    
    NSNumber *h = [dic objectForKey:@"h"];
    NSNumber *w = [dic objectForKey:@"w"];
    CGFloat h1 = [h floatValue];
    CGFloat w1 = [w floatValue];
    
    
    CGFloat height = self.view.frame.size.width * h1 / w1;
    
    
    
    
    return height + label.frame.size.height + 58;

}










@end
